package com.zt.rongda.employee.service;

import java.text.SimpleDateFormat;
import java.util.List;

import com.zt.rongda.employee.inter.EmpContractMapper;
import com.zt.rongda.employee.model.EmpContract;
import com.zt.system.model.BusinessException;
import com.zt.servlet.ReqBean;
import com.zt.system.model.SqlBuilder;
import com.zt.system.service.BaseService;
import com.zt.util.DateUtil;
import com.zt.util.StringUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class EmpContractService extends BaseService {

    @Autowired
    private EmpContractMapper empContractMapper;

    /**
    * 查询
    *
    * @param reqBean
    * @return
    */
    public JSONObject getTable(ReqBean reqBean) {
        SqlBuilder sqlBuilder = new SqlBuilder("");
        sqlBuilder.appendSql("select * from EMPLOYEE_CONTRACT where 1=1 ");

        sqlBuilder.addWhereLike2("EMP_NAME", reqBean.getInfo("empName"));
        sqlBuilder.addWhereLike2("C_STATUS", reqBean.getInfo("status"));

        // 时间间隔
        String endDate = reqBean.getInfo("makeTimeEnd");
        if(endDate != null && endDate.length()>=10)
        {
            Date tmp_date =  DateUtil.parse(endDate);

            SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
            endDate = df.format(new Date(tmp_date.getTime() + 1 * 24 * 60 * 60 * 1000)); // 结束日期加一天
        }
        sqlBuilder.addTimeWhere("MAKE_TIME", reqBean.getInfo("makeTimeStart"), ">=");
        sqlBuilder.addTimeWhere("MAKE_TIME", endDate, "<");
        //

        return queryTable(sqlBuilder, reqBean.getPageSize(), reqBean.getPageIndex());
    }

    /**
    * 通过id查询
    *
    * @param contId
    * @return
    */
    public EmpContract getById(String contId) {
        return empContractMapper.selectByPrimaryKey(contId);
    }

    /**
    * 新增
    *
    * @param empContract
    */
    public void add(EmpContract empContract) {
        empContract.setContId(stringId());// 手动设置Id

        empContract.setMakeTime(new Date());
        empContract.setModifyTime(empContract.getMakeTime());
        empContractMapper.insert(empContract);
    }

    /**
    * 修改
    *
    * @param empContract
    */
    public void update(EmpContract empContract) {
        String contId = empContract.getContId();
        if (StringUtil.isEmpty(contId)) {
            throw new BusinessException("获取主键失败");
        }

        empContract.setModifyTime(new Date());
        empContractMapper.updateByPrimaryKeySelective(empContract);
    }

    /**
    * 删除
    *
    * @param contId
    */
    public void delete(String contId) {
        if (StringUtil.isEmpty(contId)) {
            throw new BusinessException("获取主键失败");
        }
        empContractMapper.deleteByPrimaryKey(contId);
    }

}